<template>
  <section>
<!--    字典明细高级查询输入框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--明细列表-->
    <el-table :data="dictionaries" highlight-current-row v-loading="listLoading" @row-dblclick="showWords" @selection-change="selsChange" style="width: 100%;">

      <el-table-column type="selection" width="55">
      </el-table-column>

      <el-table-column type="index" label="编号" width="60" align="center">
      </el-table-column>

      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>

      <el-table-column prop="dictionary.name" label="产品类型" align="center">
      </el-table-column>

      <el-table-column label="操作" width="150" align="center">
        <template slot="header" slot-scope="scope">
          <el-button type="danger" size="small" @click="handleAdd">新增</el-button>
        </template>
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页工具条/批量删除-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>
<!--编辑/新增模态框-->
    <el-dialog class="dictionary-dialog" :title="myTitle" :visible.sync="editFormVisible" :close-on-click-modal="false"
               width="50%">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="明细名称" prop="name" >
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="明细类型" prop="aaa">
<!--          <el-input v-model="editForm.dictionaryId" auto-complete="off"></el-input>-->
          <!--
            v-for="item in cities":  循环
             :label:  展示的名字
             :value: 向后端传的值
          -->
          <el-select v-model="editForm.dictionaryId" placeholder="请选择">
            <el-option
                v-for="item in dictionariesType"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">确定</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      dictionariesType:[],//类型字典
      myTitle: "",
      filters: {
        keyword: ''  ,//类型关键字查询
        keywordDetail: ''//明细关键字查询
      },
      dictionaries: [],
      total: 0,
      pageSize: 5,
      currentPage: 1,
      listLoading: false,
      sels:[],//列表选中列
      editFormVisible: false,
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      editForm: {
        id: null,
        name: "",
        sn: "",
        dictionaryId: null,

      },
    }
  },
  methods: {
    //得到所有类型字典
    getDictionariesType(){
      this.$http.get("/dictionary/")
          .then(res=>{
            this.dictionariesType = res.data;
          })
    },
    //批量删除选择框
    selsChange: function (sels) {
      this.sels = sels;
    },
    //数据类型查询后跳转页面
    keywordQuery() {
      this.currentPage = 1;
      this.getDictionary();

    },
    //明细查询后跳转页面
    keywordDetailQuery() {
      this.currentPage = 1;
      this.getWords();

    },
    //查询当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDictionary();
    },
    handleWordCurrentChange(val) {
      this.wordDrawer.table.page.currentPage = val;
      this.getWords();
    },
    //获取字典类型列表
    getDictionary() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.filters.keyword,
      };
      this.listLoading = true;
      this.$http.post("/dictionary/word", para)
          .then(result => {
            this.listLoading = false;
            result = result.data;
            this.dictionaries = result.rows;
            this.total = result.totals;
          })
          .catch(result => {
            console.log(result);
            this.$message({
              message: '系统繁忙，稍后重试！',
              type: 'error'
            });
          })

    },
    //删除字典明细
    handleDel: function (index, row) {
      this.$confirm('你确定删除数据字典明细吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/dictionary/word/" + row.id)
            .then(result => {
              this.listLoading = false;
              result = result.data;
              if (!result.success) {
                this.$message({
                  message: result.message,
                  type: 'error'
                });
              }
              //数据删除定位到第一页
              this.currentPage = 1;
              this.getDictionary();
            })
            .catch(result => {
              console.log(result);
              this.$message({
                message: '开服前的测试！',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    },
    //删除详细字典
    handleWordDel(index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.wordDrawer.table.listLoading = true;
        this.$http.delete("/dictionary/word/" + row.id)
            .then(result => {
              this.wordDrawer.table.listLoading = false;
              result = result.data;
              if (!result.success) {
                this.$message({
                  message: result.message,
                  type: 'error'
                });
              }
              //数据删除定位到第一页
              this.wordDrawer.table.page.currentPage = 1;
              this.getWords();
            })
            .catch(result => {
              console.log(result);
              this.$message({
                message: '开服前的测试！',
                type: 'error'
              });
            })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.getDictionariesType();
      this.myTitle = "编辑数据字典";
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);//回显
    },
    //新增明细数据字典
    handleAdd: function () {
      this.getDictionariesType();
      this.myTitle = "新增字典明细";
      this.editFormVisible = true;
      this.editForm = {
        id: null,
        name: "",
        dictionaryId: null,
      };
    },
    //编辑提交类型
    editSubmit: function () {

      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editForm);
            this.$http.put("/dictionary/word/", para)
                .then(result => {
                  this.editLoading = false;
                  result = result.data;
                  if (!result.success) {
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  } else {
                    this.$message({
                      message: result.message,
                      type: 'success'
                    });
                    this.editFormVisible = false;
                  }
                  this.currentPage = 1;
                  this.getDictionary();
                })
                .catch(result => {
                  this.editLoading = false;
                  this.$message({
                    message: '系统繁忙，稍后重试！',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    //数据明细提交
    wordEditSubmit() {
      this.$refs.wordEditForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.wordDrawer.form.editLoading = true;
            let para = Object.assign({}, this.wordDrawer.form.data);
            this.$http.put("/dictionary/word", para)
                .then(result => {
                  this.wordDrawer.form.editLoading = false;
                  result = result.data;
                  if (!result.success) {
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  } else {
                    this.$message({
                      message: result.message,
                      type: 'success'
                    });
                    this.wordDrawer.form.show = false;
                  }
                  this.wordDrawer.table.page.currentPage = 1;
                  this.getWords();
                })
                .catch(result => {
                  this.wordDrawer.form.editLoading = false;
                  this.$message({
                    message: '系统繁忙，稍后重试！',
                    type: 'error'
                  });
                })
          });
        }
      });
    },
    //得到明细列表
    getWords() {
      let para = {
        ...this.wordDrawer.table.page,
        ...this.wordDrawer.query,
        ...this.wordDrawer.keywords,
      };
      this.wordDrawer.table.listLoading = true;
      this.$http.post("/dictionary/word", para)
          .then(result => {
            this.wordDrawer.table.listLoading = false;
            result = result.data;
            this.wordDrawer.table.data = result.rows;
            this.wordDrawer.table.page.total = result.total;
          })
          .catch(result => {
            console.log(result);
            this.$message({
              message: '系统繁忙，稍后重试！',
              type: 'error'
            });
          })
    },
    //批量删除
    batchRemove() {
      var ids = this.sels.map(item => item.id);  // [1,2,3,4,5,6]
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 开启忙等框

        this.$http.patch("/dictionary/word/", ids)
            .then(res => {
              // 关闭忙等框
              this.listLoading = false;
              if (res.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              this.currentPage = 1;
              this.getDictionary();
            }).catch(res => {
          this.$message({
            message: '系统异常，稍后重试',
            type: 'error'
          });
        })
      }).catch(() => {

      });
    },
  },



mounted() {
    this.getDictionary();
  }
}

</script>

<style>
.dictionary-dialog .el-dialog {
  min-width: 400px;
}
</style>